import Vue from 'vue';
import VueRouter from 'vue-router';
import { getItem, removeItem } from '@/utils/cookieUtil';
import store from '@/store';
import { Message } from 'element-ui';
import { getUserInfo } from '@/api';

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}


/**
 * 静态路由
 */
export const constantRouterMap = [
	{
		path: '/',
		redirect: '/home',
		hidden: true,
	},
	{
		path: '/home',
		name: 'Home',
		meta: {
			icon: null,
			title: '首页',
		},
		hidden: false,
		sort: null,
		component: () => import('@/views/home/index.vue'),
	},
	{
		path: '/hotel',
		name: 'Hotel',
		meta: {
			icon: null,
			title: '酒店列表',
		},
		hidden: false,
		sort: null,
		component: () => import('@/views/hotel/index.vue'),
	},
	{
		path: '/hotel/details',
		name: 'Details',
		meta: {
			icon: null,
			title: '酒店预定',
		},
		hidden: false,
		sort: null,
		component: () => import('@/views/details/index.vue'),
	},
	{
		path: '/user/center',
		name: 'UserCenter',
		meta: {
			icon: null,
			title: '用户中心',
		},
		hidden: false,
		sort: null,
		component: () => import('@/views/user/index.vue'),
	},
	{
		path: '/notice',
		name: 'Notice',
		meta: {
			icon: null,
			title: '公告',
		},
		hidden: false,
		sort: null,
		component: () => import('@/views/notice/index.vue'),
	},
	{
		path: '/message',
		name: 'Message',
		meta: {
			icon: null,
			title: '留言板',
		},
		hidden: false,
		sort: null,
		component: () => import('@/views/message/index.vue'),
	},

]
/**
 * 动态路由
 */
export const asyncRouterMap = [


]


const router = new VueRouter({
	mode: "history",
	routes: constantRouterMap,
	// // 控制路由跳转滚动条的位置为最顶部
	scrollBehavior(to, from, savedPosition) {
		// 始终滚动到顶部，y代表距离顶部的距离
		return { y: 0 }
	},
})

// const whiteList = ['/login', '/file/share/'] // 不重定向白名单

// router.beforeEach((to, from, next) => {
// 	if (getItem("token")) {
// 		if (store.state.user.userInfo == null) {
// 			// 拉取用户信息
// 			getUserInfo(next);
// 		}
// 		if (to.path.match('/backend/admin') && store.state.user.roleList.length === 1) {
// 			next({ path: '/' })
// 		}
// 		next()
// 	} else {
// 		next();
// 		// if (to.path == '/login') {
// 		// 	next()
// 		// } else {
// 		// 	next("/login")
// 		// }
// 	}
// 	next()
// })

router.beforeEach((to, from, next) => {
	next()
	if (getItem("Authorization")) {
		if (store.state.user.userInfo == null) {
			getUserInfo().then(res => {
				store.dispatch('setUserInfo', res.user)
				localStorage.setItem('userInfo', JSON.stringify(res.user));
				next();
			}).catch(err => {

			})
		} else {
			if (to.path.match('/backend/admin') && store.state.user.roleList.length === 1) {
				next({ path: '/' })
			} else {
				next();
			}
		}
	} else {
		if (to.path == '/usersapce') {
			next();
		} else {
			next();
		}
	}
})

router.afterEach(() => {

});

function getUserInfo0(next) {
	return new Promise((resolve, reject) => {
		reqUser().then(res => {
			store.dispatch('setUserInfo', res.data)
				.then(() => {
					return store.dispatch('generateRoutes', JSON.parse(localStorage.getItem('menus')));
				})
				.then(() => {
					// 动态添加可访问路由表  
					router.addRoutes(store.state.menu.addRouters);
					resolve();
				})
				.catch((error) => {
					console.error('处理流程出错:', error);
					// 处理错误，例如跳转到错误页面或重新登录
					next({ path: '/' })
				});
		}).catch(error => {
			next({ path: '/' })
			Message.error({
				message: error.msg
			});
			removeItem("Authorization");


			// if (error.code == 500 && error.msg == '身份解析失败') {
			// 	removeItem("token");
			// 	Message.error({
			// 		message: "身份过期，请重新登录"
			// 	});
			// } else {
			// 	Message.error({
			// 		message: error.msg
			// 	});
			// }
		})
	});
}




export default router